<?php


namespace app\zhongcan\controller;


use app\zhongcan\model\NewsModel;
use app\zhongcan\validate\NewsValidate;
use cmf\controller\AdminBaseController;

class NewsController extends AdminBaseController
{
    //页面显示
    public function index(){
        $keyword = input('keyword','');
        $where = [];
        if($keyword){
            $where[] = ['title|author', 'like', "%$keyword%"];
        }
        $list = NewsModel::order(['list_order' => 'DESC'])
            ->where($where)
            ->paginate(2);
        $this->assign('list',$list);
        return $this->fetch();
    }
    //添加页面显示
    public function add(){
        return $this->fetch();
    }
    //添加方法
    public function addPost(){
        $data = input('post.');
        $content = $data['content'];
        $content1 = htmlspecialchars_decode($content);
        $content2 = cmf_replace_content_file_url($content1,true);
        $content3 = htmlspecialchars($content2);
        $data['content'] = $content3;
        $validate = new NewsValidate();
        $res = $validate->scene('add')->check($data);
        if(!$res){
            $this->error($validate->getError());
        }
//        $data['create_time'] = date('Y-m-d H:i:s');
//        if(NewsModel::create($data)){
//            $this->success('添加成功','index');
//        }else{
//            $this->error('添加失败');
//        }
//        $image = $data['image'];
//        $url = cmf_get_image_url($image);
//        $add = NewsModel::create([
//            'title' => $data['title'],
//            'image' => $url,
//            'author' => $data['author'],
//            'look_num' => $data['look_num'],
//            'abstract' => $data['abstract'],
//            'content' => $content3,
//            'create_time' => date('Y-m-d H:i:s'),
//        ]);
//        if($add){
//            $this->success('添加成功','index');
//        }else{
//            $this->error('添加失败');
//        }
    }
    //修改页面显示
    public function edit(){
        $id = input('id');
        $news = NewsModel::find($id);
        $news['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($news['content']));
        $this->assign('news',$news);
        return $this->fetch();
    }
    //修改方法
    public function editPost(){
        $data = input('post.');
        $content = $data['content'];
        $content1 = htmlspecialchars_decode($content);
        $content2 = cmf_replace_content_file_url($content1,true);
        $content3 = htmlspecialchars($content2);
        $data['content'] = $content3;
        //验证器
        $validate = new NewsValidate();
        $res = $validate->scene('edit')->check($data);
        if(!$res){
            $this->error($validate->getError());
        }
//        查询用户是否存在
        $edit = NewsModel::find($data['id']);
        if(!$edit){
            $this->error('未查询到该数据');
        }
        $edit->title= $data['title'];
        $edit->image= $data['image'];
        $edit->look_num= $data['look_num'];
        $edit->author= $data['author'];
        $edit->abstract= $data['abstract'];
        $edit->content= $data['content'];
        $data['update_time'] = date('Y-m-d H:i:s');
        if ($edit->save()) {
            $this->success('修改成功','News/index');
        } else {
            $this->error('修改失败');
        }
//        //查询该用户是否存在
//        if(!NewsModel::find($data['id'])){
//            $this->error('该用户不存在');
//        }
//        $model = new NewsModel();
//        $model->title =$data['title'];
//        if(NewsModel::save($data)){
//            $this->success('保存成功','index');
//        }else{
//            $this->error('保存失败');
//        }
    }
    //单条删除
    public function delete(){
        $id = input('id/d','');
        if (!$id){
           $this->error('参数错误');
        }
       if (NewsModel::destroy($id)){
           $this->success('删除成功');
       }else{
           $this->error('删除失败');
       }
    }
    //排序
    public function order(){
        parent::listOrders(new NewsModel());
        $this->success('排序成功');
    }
    //预览文章
    public function content(){
        $id = input('id/d');
        if(empty($id)){
            $this->error('参数错误');
        }
        $list = NewsModel::find($id);
        $list['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($list['content']));
        $this->assign('list',$list);
        return $this->fetch('detail');
    }
    //开关
    public function change(){
        $id = input('id');
        if(!$id){
            $this->error('参数错误');
        }
        $new = NewsModel::find($id);
        if($new['switch'] == 0){
            $newSwitch = 1;
        }else{
            $newSwitch = 0;
        }
        $new->switch = $newSwitch;
        if($new->save()){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }
}